package com.example.xiaozhi.demo;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btnInsersStu;
    private Button btnQueryStu;
    private Button btnDelStu;
    private Button btnUpdateStu;
    private Button btnInsersSco;
    private Button btnQuerySco;
    private TextView txtResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnInsersStu=findViewById(R.id.btn_insert_stu);
        btnInsersStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Uri uri=Uri.parse("content://com.gdcp.student/student");
                ContentValues values=new ContentValues();
                values.put("stuno","17101");
                values.put("name" ,"xz");
                values.put("age", 20);
                getContentResolver().insert(uri,values);

            }
        });
        txtResult=findViewById(R.id.txt_result);
        btnQueryStu=findViewById(R.id.btn_query_stu);
        btnQueryStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri=Uri.parse("content://com.gdcp.student/student");
            Cursor cursor=getContentResolver().query(uri,null,null,null,null);
              if (cursor!=null&&cursor.getCount()>0){
                  StringBuffer buffer=new StringBuffer();
                  for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                      String stuno=cursor.getString(0);
                      String name=cursor.getString(1);
                      int age=cursor.getInt(2);
                      buffer.append(stuno).append(" ").append(name).append(" ").append(age).append("");
                  }
                  txtResult.setText(buffer);
              }else{
                  txtResult.setText("已清空");
              }
            }
        });
        btnDelStu=findViewById(R.id.btn_del_stu);
        btnDelStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                String where="name='xz'";
                int  rows=getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"删除了"+rows+"行",Toast.LENGTH_SHORT).show();
            }
        });

        btnUpdateStu=findViewById(R.id.btn_update_stu);
        btnUpdateStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                ContentValues contentValues=new ContentValues();
                contentValues.put("age",25);
                String where="age=20";
                int rows=getContentResolver().update(uri,contentValues,where,null);
                Toast.makeText(MainActivity.this,"更新了"+rows+"行",Toast.LENGTH_SHORT).show();
            }
        });

        btnInsersSco=findViewById(R.id.btn_insert_sco);
        btnInsersSco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("content://com.gdcp.student/score");
                ContentValues values=new ContentValues();
                values.put("stuno","17101");
                values.put("english" ,84);
                values.put("android", 80);
                getContentResolver().insert(uri,values);

            }
        });

        btnQuerySco=findViewById(R.id.btn_query_sco);
        btnQuerySco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri=Uri.parse("content://com.gdcp.student/score");
                Cursor cursor=getContentResolver().query(uri,null,null,null,null);
                if (cursor!=null&&cursor.getCount()>0){
                    StringBuffer buffer=new StringBuffer();
                    for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        String stuno=cursor.getString(0);
                        int english=cursor.getInt(1);
                        int android=cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(english).append(" ").append(android).append("  ");
                    }
                    txtResult.setText(buffer);
                }
            }
        });


    }
}
